<?php

class UserImage
{
    public static $null = "";

    private static $category;
    private static $config;

    public static function getConfig($category)
    {
        if(self::$category==$category) {
            return self::$config;
        }
    
        $config=Yii::app()->params->userImage[$category];
        if(!isset($config))
            throw new CException("userImage $category does not exist");
        
        self::$category=$category;
        self::$config=$config;
        return $config;
    }
    
    public static function validateUploadFormat($category, $format)
    {
        $config=self::getConfig($category);
        return $config['uploadFormats'][strtolower($format)]?true:false;
    }
    
    public static function toUrl($category, $files, $type=null)
    {    
        $config=self::getConfig($category);
        if(is_array($files)) {
            if($type===null) {
                $type=$config['defaultImage'];
            }
            $filename=$files[$type];
        }
        else {
            $filename=$files;
        }

        if(!$filename){
            $params=$config['images'][$type];
            return isset($params['defaultUrl'])?$params['defaultUrl']:'';
        }
        else {
            $url=sprintf($config['baseUrl'], hexdec($filename[0]));
            $dir=sprintf($config['dir'],$filename[0],$filename[1],$filename[2],$filename[3]);
            return $url.$dir.$filename;
        }
    }
    
    public static function toPath($category, $files, $type=null)
    {
        $config=self::getConfig($category);
        if(is_array($files)) {
            if($type===null) {
                $type=$config['defaultImage'];
            }
            $filename=$files[$type];
        }
        else {
            $filename=$files;
        }
        $dir=sprintf($config['dir'],$filename[0],$filename[1],$filename[2],$filename[3]);
        return $config['basePath'].$dir.$filename;
    }
    
    public static function create($category, $srcFile)
    {
        $config=self::getConfig($category);
        $imageConfig = $config['images'];
        $files=array();
        foreach($imageConfig as $type=>$params) {        

            $filename = self::createOneImage($category, $srcFile, $params);
            if($filename===false) {
                self::delete($category, $filename);
                return false;
            }

            $files[$type]=$filename;
        }
        return $files;
    }

    public static function createOneImage($category, $srcFile, $params)
    {
        $format=isset($params['format'])?$params['format']:'jpg';
        $filename=self::randomFilename($category).'.'.$format;
        $filePath=self::toPath($category, $filename);

        $operations=$params['operations'];
        $operations[]=array('save',
                'fileName'=>$filePath,
                'format'=>$format);

        return ImageProcessor::process($srcFile,$operations,$params)?$filename:false;
    }
    
    public static function delete($category, $filename)
    {
        if(!$filename) return;
        if(is_array($filename)) {
            foreach($filename as $file) {
                unlink(self::toPath($category, $file));
            }
        }
        else {
            unlink(self::toPath($category, $filename));        
        }
    }
    
    private static function randomFilename($category)
    {
        return md5($category.'-'.microtime().'-'.posix_getgid().'-'.mt_rand());
    }    
}

?>